﻿var ChangePasswordPage = function(pageContainer) {

    ChangePasswordPage.superclass.constructor.call(this, pageContainer);

    this.CanGoBack = false;

    var self = this;
    var Elements = {
        currPsw: pageContainer.find(".currentPassword"),
        newPsw: pageContainer.find(".newPassword"),
        confNewPsw: pageContainer.find(".confNewPassword"),
        cancel: pageContainer.find(".cancel"),
        savePsw: pageContainer.find(".savePsw")
    };

    self.initialize = override(self.initialize, function() {
        self.Title = translate('Module_Title_ChangePassword');
        app.translatePage(app.Pages.ChangePassword);

        self.resetFields();

        Elements.cancel.tap(function() {
            self.resetFields();
            app.goBack();
        });

        Elements.savePsw.click(function() {
            var currPsw = Elements.currPsw.val();
            if (currPsw.length == 0)
                return app.MessagesFactory.showToastWarning(translate('General_Eroare'), translate('ChangePassword_InsertCurrentPassword'));
            var newPsw = Elements.newPsw.val();
            if (newPsw.length == 0)
                return app.MessagesFactory.showToastWarning(translate('General_Eroare'), translate('ChangePassword_InsertNewPassword'));
            var confNewPsw = Elements.confNewPsw.val();
            if (confNewPsw.length == 0)
                return app.MessagesFactory.showToastWarning(translate('General_Eroare'), translate('ChangePassword_ConfirmNewPassword'));
            if (newPsw != confNewPsw)
                return app.MessagesFactory.showToastWarning(translate('General_Eroare'), translate('ChangePassword_PasswordsError'));

            app.showLoadingScreen();
            app.TMMService.Security.ChangePassword(currPsw, newPsw, function(response) {
                app.hideLoadingScreen();
                if (response && response.FinalResult) {
                    app.MessagesFactory.showToastSuccess(translate('General_Succes'), translate('ChangePassword_SaveSuccessful'));
                    self.resetFields();
                } else {
                    if (response.Exception.Code) {
                        var code = "translate('MyProfile_SchimbareParola_Eroare_" + response.Exception.Code;
                        var msg = eval(code);
                        app.MessagesFactory.showToastWarning(translate('General_Eroare'), msg);
                    } else app.MessagesFactory.showToastWarning(translate('General_Eroare'), translate('ChangePassword_IncorectPassword'));
                }
            });
        });

        pageContainer.bind('onBeforePageShow', function() {
            app.Phone.setBackbtnVisible(true);
        });
    });

    this.resetFields = function() {
        Elements.currPsw.val("");
        Elements.newPsw.val("");
        Elements.confNewPsw.val("");
    };

    this.setModuleAction = override(self.setModuleAction, function() {
        if (app.Phone)
            app.Phone.setModuleAction(ModuleActions.None);
    });

    self.initialize();
};
extend(ChangePasswordPage, TMMobilePage);